package com.zdxlz.fcmp.upms.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zdxlz.fcmp.common.mybatis.enums.UserTypeEnum;
import com.zdxlz.fcmp.upms.api.entity.SystemRole;
import com.zdxlz.fcmp.upms.api.entity.SystemTenant;
import com.zdxlz.fcmp.upms.api.entity.SystemUser;
import com.zdxlz.fcmp.upms.api.mapper.SystemMenuMapper;
import com.zdxlz.fcmp.upms.api.mapper.SystemRoleMapper;
import com.zdxlz.fcmp.upms.api.mapper.SystemTenantMapper;
import com.zdxlz.fcmp.upms.api.service.ISystemCommonService;
import com.zdxlz.fcmp.upms.api.service.ISystemMenuService;
import com.zdxlz.fcmp.upms.api.vo.SystemMenuListVo;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zdxlz.fcmp.common.core.constant.CommonConstants.*;

/**
 * @author: aman
 * @className SystemCommonServiceImpl
 * @date 2025/2/5 11:02
 * @description
 */
@Service
@AllArgsConstructor
public class SystemCommonServiceImpl implements ISystemCommonService {

    @Resource
    private SystemRoleMapper systemRoleMapper;

    @Resource
    private SystemTenantMapper systemTenantMapper;

    @Resource
    private ISystemMenuService systemMenuService;

    @Resource
    private SystemMenuMapper systemMenuMapper;

    /**
     * @description：获取有效的角色信息，返回List<Map>集合
     * @author：aman
     * @date：2025/2/25 17:03
     */
    @Override
    public List<Map<String, Object>> getUserRoleInfo(SystemUser systemUser) {
        List<Map<String, Object>> ruleLists = new ArrayList<>();
        LambdaQueryWrapper<SystemRole> queryWrapper = new LambdaQueryWrapper<>();
        switch (systemUser.getUserType()) {
            case TENANT, USER -> queryWrapper.eq(SystemRole::getTenantId, systemUser.getTenantId());
        }
        List<SystemRole> roleList = systemRoleMapper.selectList(queryWrapper);
        roleList.forEach(role -> {
            Map<String, Object> map = new HashMap<>();
            map.put(ID, role.getId());
            map.put(ROLE_NAME, role.getName());
            map.put(STATUS, role.getStatus());
            ruleLists.add(map);
        });
        return ruleLists;
    }

    /**
     * @description：获取所有的租户信息，返回List<Map>集合，新增用户使用
     * @author：aman
     * @date：2025/2/25 17:03
     */
    @Override
    public List<Map<String, Object>> getUserLinkTenantInfo() {
        List<Map<String, Object>> tenantLists = new ArrayList<>();
        List<SystemTenant> tenantList = systemTenantMapper.selectList(new LambdaQueryWrapper<>());
        tenantList.forEach(tenant -> {
            Map<String, Object> map = new HashMap<>();
            map.put(ID, tenant.getId());
            map.put(TENANT_NAME, tenant.getName());
            tenantLists.add(map);
        });
        return tenantLists;
    }

    /**
     * @description：获取有效当前登陆人有权限得的菜单信息，返回List<>集合，新增角色使用
     * @author：aman
     * @date：2025/2/25 17:03
     */
    @Override
    public List<SystemMenuListVo> getUserMenuInfo(SystemUser systemUser) {
        List<String> permissionCodes;
        if (systemUser.getUserType().equals(UserTypeEnum.SUPPER)) {
            permissionCodes = systemMenuMapper.getPermissionByUserId(null);
        } else {
            permissionCodes = systemMenuMapper.getPermissionByUserId(systemUser.getId());
        }
        return systemMenuService.queryMenuList(permissionCodes);
    }
}
